//
//  ChainNewsBottomShareBar.m
//  Chain
//
//  Created by zhaosh on 2018/4/16.
//  Copyright © 2018年 zhaosh. All rights reserved.
//
#import "ChainNewsBottomShareBar.h"
#import "UIButton+LXMImagePosition.h"

@interface ChainNewsBottomShareBar ()

@property(copy,nonatomic) NSArray *icons;
@property(copy,nonatomic) NSArray *titles;
@end

@implementation ChainNewsBottomShareBar
-(instancetype )initWithFrame:(CGRect)frame iconsArr:(NSArray <NSString *> *)iconsArr titles:(NSArray <NSString *> *)titlesArr{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.6];
        
        self.titles = titlesArr;
        self.icons = iconsArr;
        
        [self setupSubviews];
    }
    return self;
}

-(void)setupSubviews{
    CGFloat h = 170;
    UIView *bottomV = [[UIView alloc]initWithFrame:CGRectMake(0, self.height - h, self.width, h)];
    bottomV.backgroundColor = HEXCOLOR(@"#fafafa");;
    [self addSubview:bottomV];

    CGFloat marL = 50;
    CGFloat btnW = 55;
    CGFloat btnH = 55;
    CGFloat marBetw = (self.width - 2 * marL - btnW * self.icons.count) / (self.icons.count - 1);
    CGFloat marT = 25;
    
    for (NSInteger index = 0;index < self.icons.count;index++) {
        NSString *image = self.icons[index];
        
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(marL + (marBetw + btnW)* index, marT, btnW, btnH)];
        [btn setImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
        
        btn.backgroundColor = [UIColor clearColor];
        btn.layer.cornerRadius = 13.;
        btn.layer.masksToBounds = YES;
        [btn setTitleColor:LableTextColor666 forState:UIControlStateNormal];
        btn.titleLabel.font = FONT_LB(12);
        [bottomV addSubview:btn];
        btn.tag = index;
        [btn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    UIView *lineView=[[UIView alloc] initWithFrame:CGRectMake(20, self.height-65, self.width-40, 0.5)];
    lineView.backgroundColor= HEXCOLOR(@"#e6e6e6");
    [self addSubview:lineView];
    
    UITapGestureRecognizer *t = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap)];
    [self addGestureRecognizer:t];
    
    UIButton *cancelBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, self.height - 52, self.width, 44)];
    cancelBtn.backgroundColor = [UIColor clearColor];
    cancelBtn.titleLabel.font = FONT_LB(15);
    [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [cancelBtn setTitleColor:HEXCOLOR(@"#999999") forState:UIControlStateNormal];
    [cancelBtn addTarget:self action:@selector(cancelBtnClicked) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:cancelBtn];
}

-(void)btnClicked:(UIButton *)btn{
    if (self.clickedBlock) {
        self.clickedBlock(btn.tag);
    }
    [self tap];
}

-(void)cancelBtnClicked{
    [self tap];
}

-(void)tap{
    [self removeFromSuperview];
    if(self.closeBlock){
        self.closeBlock();
    }
}
@end
